using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace TechHeadBrothers.IO
{
    public static class ZipUtilities
    {
        /// <summary>
        /// Saves the zip.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="zipPath">The zip path.</param>
        /// <param name="zipLevel">The zip level.</param>
        /// <returns></returns>
        public static bool SaveZip(List<string> files, string zipPath, int zipLevel)
        {
            bool isSaved;

            FileStream fZip = null;
            ZipOutputStream zipOStream = null;

            try
            {
                fZip = File.Create(zipPath);
                zipOStream = new ZipOutputStream(fZip);
                zipOStream.SetLevel(zipLevel);

                AddFilesToZip(files, zipOStream);

                isSaved = true;
            }
            catch (Exception ex)
            {
                isSaved = false;
            }
            finally
            {
                if (zipOStream != null)
                    zipOStream.Close();

                if (fZip != null)
                    fZip.Close();
            }

            return isSaved;
        }

        /// <summary>
        /// Adds the files to zip.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="zipOStream">The zip O stream.</param>
        public static void AddFilesToZip(List<string> files, ZipOutputStream zipOStream)
        {
            foreach (string file in files)
            {
                FileStream fs = File.OpenRead(file);

                byte[] tampon = new byte[fs.Length];
                fs.Read(tampon, 0, tampon.Length);

                if (File.Exists(file))
                {
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    zipOStream.PutNextEntry(entry);
                    zipOStream.Write(tampon, 0, tampon.Length);
                }

                fs.Close();
            }
        }

        /// <summary>
        /// Gets the zip as byte array.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="zipLevel">The zip level.</param>
        /// <returns></returns>
        public static byte[] GetZipAsByteArray(List<string> files, int zipLevel)
        {
            byte[] bytes;

            using (MemoryStream inMemoryStream = new MemoryStream())
            {
                ZipOutputStream zipOStream = new ZipOutputStream(inMemoryStream);

                zipOStream.SetLevel(zipLevel);

                try
                {
                    AddFilesToZip(files, zipOStream);
                }
                finally
                {
                    zipOStream.Finish();
                    zipOStream.Close();
                }

                bytes = inMemoryStream.ToArray();
            }

            return bytes;
        }
    }
}